//
//  ZDMainController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/13.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDMainController.h"
#import "UIViewController+MMDrawerController.h"
#import "ZDHeaderView.h"
#import "CollectionCell.h"
#import "NSDictionary+ToArray.h"
#import "ZDGoodsModel.h"
#import "ZDCollectionHearderView.h"

#import "ZDSearchController.h"
#import "ZDServiceController.h"
#import "ZDShoppCarController.h"
#import "ZDPoiController.h"
#import "ZDOrderCenterController.h"
#import "ZDMyWalletController.h"
#import "ZDMessageController.h"

static NSString *const myworkCell = @"myworkCell";
static NSString *const ZDCollectionHearderViewId = @"ZDCollectionHearderView";

@interface ZDMainController ()<UICollectionViewDelegateFlowLayout>

// 图片数据源
@property(nonatomic,strong)NSArray* imageArray;

// 标题数据源
@property(nonatomic,strong)NSArray* titleArray;

// searchButton
@property(nonatomic,strong)UIButton *searchButton;

@end

@implementation ZDMainController

- (void)setupUI
{
    [super setupUI];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"leftMenue"] style:UIBarButtonItemStylePlain target:self action:@selector(leftClick:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    self.navigationItem.titleView = self.searchButton;
    
    //header
    ZDHeaderView *headerView = (ZDHeaderView *)[UIView getNibView:@"ZDHeaderView"];
    headerView.frame = CGRectMake(0, 0, LEScreenWidth, 105);
    [self.view addSubview:headerView];
     __weak typeof (self) weakSelf = self;
    [headerView headerViewClick:^(NSInteger tag) {
       ZDLog(@"%s---%d---%ld",__func__,__LINE__,tag);
        if (![[NSUserDefaults standardUserDefaults] boolForKey:ISLOGIN])
        {
            [self presentLoginController];
            return ;
        }
        if (tag == 3) {
            ZDShoppCarController *shopCar = [[ZDShoppCarController alloc] init];
            [weakSelf.navigationController pushViewController:shopCar animated:YES];
        }else if (tag == 2)
        {
            [weakSelf pushOrderCenter];
        }else if (tag == 1){
            ZDMyWalletController *wallet = [[ZDMyWalletController alloc]init];
            [weakSelf.navigationController pushViewController:wallet animated:YES];
        }else if (tag == 4){
            ZDMessageController *message = [[ZDMessageController alloc] init];
            [weakSelf.navigationController pushViewController:message animated:YES];
        }
    }];
    
    [self resetCollectionViewFrame:CGRectMake(0, headerView.bottom, LEScreenWidth, LEScreenHeight - 64 -105)];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionCell" bundle:nil] forCellWithReuseIdentifier:myworkCell];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZDCollectionHearderView" bundle:nil] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:ZDCollectionHearderViewId];
    [self initHeader];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.barTintColor = NavigationBlueColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}
- (void)loadData
{
    NSArray *imageArr = @[@"m_yinhang",@"m_shangchang",@"m_yiyuan",@"m_yaodian"];
    _imageArray = imageArr;
    
    NSArray *titleArr = @[@"银行",@"商场",@"医院",@"药店"];
    _titleArray = titleArr;
    [self requestRefreshData];
}

#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"0" forKey:@"gc_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API netAccess:MainGetServiceList andMethor:@"GET" andDict:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        if (error)
        {
            [SVProgressHUD showInfoWithStatus:@"暂无数据"];
        }else
        {
            NSDictionary *goodsListDict = datas[@"show_goods_class"];
            NSMutableArray *tempArray = [goodsListDict toArray:goodsListDict];
            NSArray *tempModel = [ZDGoodsModel mj_objectArrayWithKeyValuesArray:tempArray];
            
            [weakSelf.dataSource removeAllObjects];
            [weakSelf.dataSource addObjectsFromArray:tempModel];
        }
        [weakSelf.collectionView reloadData];
        
    } falure:^(NSError *er) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showInfoWithStatus:@"网络错误"];
    }];

}


#pragma mark- collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
       return self.dataSource.count;
    }
    return 4;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return  CGSizeZero;
    }
    return CGSizeMake(LEScreenWidth, 110);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    if (kind == UICollectionElementKindSectionHeader)
    {
        if (indexPath.section == 1)
        {
            ZDCollectionHearderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ZDCollectionHearderViewId forIndexPath:indexPath];
            return headerView;
        }
            return nil;
    }
    return nil;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:myworkCell forIndexPath:indexPath];
    if (indexPath.section == 0)
    {
       cell.model = self.dataSource[indexPath.row];
    }
    if (indexPath.section == 1)
    {
        NSString *imageStr = _imageArray[indexPath.row];
        cell.icon.image = [UIImage imageNamed:imageStr];
        cell.titleLbl.text = _titleArray[indexPath.row];
    }
    cell.rightLineView.hidden = (indexPath.row + 1)%4 == 0 ? YES : NO;
    return  cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        ZDServiceController *service = [[ZDServiceController alloc] init];
        service.model = self.dataSource[indexPath.row];
        [self.navigationController pushViewController:service animated:YES];
    }
    if (indexPath.section == 1)
    {
        ZDPoiController *poi = [[ZDPoiController alloc] init];
        poi.poiString = self.titleArray[indexPath.row];
        [self.navigationController pushViewController:poi animated:YES];
    }
}
#pragma mark- 点击事件
- (void)leftClick:(UIButton *)sender
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
- (void)searchClick
{
    ZDLog(@"%s---%d---点击搜索",__func__,__LINE__);
    if (![[NSUserDefaults standardUserDefaults] boolForKey:ISLOGIN])
    {
        [self presentLoginController];
        return ;
    }
    ZDSearchController *searchController = [[ZDSearchController alloc] init];
    [self.navigationController pushViewController:searchController animated:YES];
}
- (void)pushOrderCenter
{
    ZDOrderCenterController *orderCenter = [[ZDOrderCenterController alloc] init];
    orderCenter.menuViewStyle = WMMenuViewStyleLine;
    orderCenter.selectIndex = 0;
    orderCenter.menuHeight = 40;
    orderCenter.titleColorSelected = [UIColor blackColor];
    orderCenter.titleColorNormal = LEHaxColor(0x808080);
    orderCenter.progressWidth = orderCenter.menuItemWidth;
    orderCenter.menuBGColor = [UIColor whiteColor];
    orderCenter.titleSizeSelected = 15;
    orderCenter.progressColor = LEHaxColor(0x72bd18);
    [self.navigationController pushViewController:orderCenter animated:YES];
    
}
#pragma mark- getter
- (UIButton *)searchButton
{
    if (_searchButton == nil) {
        _searchButton = [[UIButton alloc] initWithFrame:CGRectMake(70, 5, LEScreenWidth -90, 30)];
        [_searchButton setTitle:@"搜一搜" forState:0];
        [_searchButton setBackgroundColor:LEHaxColor(0xe7ebf1)];
        [_searchButton setTitleColor:LEHaxColor(0x75787a) forState:0];
        [_searchButton.layer setCornerRadius:8];
        _searchButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _searchButton.layer.borderColor = LEHaxColor(0xc0c3cd).CGColor;
        _searchButton.layer.borderWidth = 1.0;
        [_searchButton addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchButton;
}
@end





